package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Collect;
import com.net.tkc.pro.entity.PaiH;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.UserIntegral;
import com.net.tkc.pro.impl.CollectImpl;
import com.net.tkc.pro.impl.UserIntegralImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/UserIntegral/")
public class UserIntegralController {
    ResultEntity resultEntity = null;
    @Resource
    UserIntegralImpl userIntegralImpl;

    /**
     * 无条件查询表中信息用于后台管理系统
     * @return
     */
    @GetMapping("GetList")
    private String getSun(
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        resultEntity = new ResultEntity();
        try{
            int Start =(page-1)*limit;
            List<UserIntegral> list=userIntegralImpl.getLIst(limit, Start);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("GetSum")
    private String getSun(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            int result = userIntegralImpl.getSun(userid);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("获取积分成功！");
                resultEntity.setData(result);
            }else{
                resultEntity.setCode("1");
                resultEntity.setData(result);
                resultEntity.setMsg("用户积分为零！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 通过用户id查找积分多少
     * @param userid
     * @return
     */
    @GetMapping("UserIntegral")
    private String getUserList(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<UserIntegral> list=userIntegralImpl.getUserList(userid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据id删除积分
     * @param id
     * @return
     */
    @DeleteMapping("Int")
    private String delint(
            @RequestParam("id") int id
    ) {
        resultEntity = new ResultEntity();
        try {
            int result = userIntegralImpl.delint(id);
            if (result > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("失败！请联系管理员");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 修改用户积分详情和积分数量
     * @param id
     * @param mode
     * @param fraction
     * @return
     */
    @PutMapping("Int")
    private String upInt(
            @RequestParam("mode") String mode,
            @RequestParam("id") int id,
            @RequestParam("fraction") int fraction
    ) {
        resultEntity = new ResultEntity();
        try {
            int result = userIntegralImpl.upInt(id, mode, fraction);
            if (result > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("失败！请联系管理员");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

        /**
         * 添加用户获取积分方式
         * @param mode  积分获取方式
         * @param userid 用户id
         * @param fraction 获取积分多少
         * @return
         */
    @PostMapping("UserIntegral")
    private String addUserIntegral(
            @RequestParam("mode") String mode,
            @RequestParam("userid") int userid,
            @RequestParam("fraction") int fraction
    ){
        resultEntity = new ResultEntity();
        UserIntegral userIntegral=new UserIntegral();
        userIntegral.setUserid(userid);
        userIntegral.setMode(mode);
        userIntegral.setFraction(fraction);
        try{
            int result = userIntegralImpl.addUserIntegral(userIntegral);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @GetMapping("GetPaiH")
    private String getSunLIst(
    ){
        resultEntity = new ResultEntity();
        try{
            List<PaiH> list=userIntegralImpl.getSunLIst();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
